<?php

class BuyNowAction extends Action
{
    /**
     * 首页
     */
    public function index()
    {
        $postStr = file_get_contents("php://input");
        $array = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
        $orderid = $array['out_trade_no'];
        $openid = $array['openid'];
        $total_fee = $array['total_fee'];
        $attach = explode(',', $array['attach']);
        $createtime = time();
        if ($array['result_code'] == 'SUCCESS' && $array['return_code'] == 'SUCCESS') {
            //保存订单数据
            $userid = M('Wxuser')->where(array('openid'=>$openid))->getField('id');
            $data = array();
            $data['userid'] = $userid;
            $data['orderid'] = $orderid;
            $data['openid'] = $openid;
            $data['money'] = $total_fee/100;
            $data['score'] = $attach[0];
            $data['createtime'] = $createtime;
            $data['awardid'] = $attach[1];
            M('BuyNowRecord')->data($data)->add();

            M('BuyNowList')->where(array('id'=>$attach[1]))->setInc('draw');
           
            $award = M('BuyNowList')->where(array('id'=>$attach[1]))->find();
            if($award['type']=='card'){
                $name = M('card')->where(array('id'=>$award['name']))->getField('title');
            }else{
                $name = $award['name'];
            }
             //积分变动记录
            $pointChangeModel=D('PointChange');
            $data=array();
            $data['openid']=$openid;
            $data['createtime']=time();
            $data['change']=0-$attach[0];
            $data['reason']='BuyNow';
            $data['exchangeid']=$attach[1];
            $pointChangeModel->data($data)->add();

            $this->sendMessage('秒杀成功','成功秒杀'.$name);

            if($award['type']=='card'){
                $getModel=D('UserGetCard');
                $cardModel = D('Card');
                $cardInfo=$cardModel->where(array('id'=>$award['name']))->find();
                $data=array();
                $data['wxuserid']=D('Wxuser')->where(array('openid'=>$openid))->getField('id');
                $data['cardid']=$cardInfo['cardid'];
                $data['createtime']=time();
                $data['code']=$this->getCode();
                $data['status']=0;
                $getcardid=$getModel->data($data)->add();

                //把usergetcard表的id记录到表awardresult中
                // $arModel->where(array('id'=>$awardResultId))->data(array('getcardid'=>$getcardid))->save();
            }

            echo '<xml> <return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[OK]]></return_msg> </xml>';
            
        }

    }

    /**
     * 生成一个没有重复的code码
     */
    public function getCode()
    {
        $resultModel = D('UserGetCard');
        do {
            $str = rand_string(12, 1);
            $map = array();
            $map['code'] = $str;
            $rs = $resultModel->where($map)->find();
        } while ($rs);  //如果存在，$rs非空，那么就重新获取
        return $str;
    }

    public function sendMessage($title,$content){
        $messageModel=D('Message');
        $data=array();
        $data['openid']=session::get('openid');
        $data['createtime']=time();
        $data['content']=$content;
        $data['title']=$title;
        // $data['name']=$name;
        $data['read']=0;
        $messageModel->data($data)->add();
    }

    public function test(){
        $m = M('shop');
        for ($i=101; $i <=1000 ; $i++) { 
            $map = array();
            $map['id'] = $i;
            $data=array();
            $data['salenum'] = 0;
            $m->where($map)->data($data)->save();
        }
    }

}